var underscore = require('underscore')

var monster = function(opts) {
    //模型id
    this.id = opts.id || 0
    //角色名字
    this.name = opts.name
    //类型 1:人物2:宠物,3：怪物,4: boss
    this.type = opts.type || 3
    //战斗属性
    this.fightAttri = opts.fightAttri ? new FightAttri(opts.fightAttri) : new FightAttri({})
    //基础属性
    this.basicAttri = opts.basicAttri ? new BasicAttri(opts.basicAttri) : new BasicAttri({})
    //技能组
    this.skills = opts.skills || []
    //当前等级
    this.lvl = opts.lvl || 1
    //获得经验
    this.getexp = opts.getexp || 0
    //获得金钱
    this.getmoney = opts.getmoney || 0
    //获得物品
    this.dropItems = opts.dropItems || {}
    //怪物前缀属性
    this.prefixAttri = opts.prefixAttri || [];
}

//基础属性
function BasicAttri(opts) {
    //血量
    this.maxHp = opts.maxHp || 0
    //攻击力
    this.attack = opts.attack || 0
    //防御
    this.defend = opts.defend || 0
    //暴击伤害
    this.critHurt = opts.critHurt || 0
    //暴击
    this.crit = opts.crit || 0
    //韧性
    this.toughness = opts.toughness || 0
    //命中
    this.hit = opts.hit || 0
    //闪避
    this.avoid = opts.avoid || 0
    //控制
    this.control = opts.control || 0
    //抗性
    this.resistance = opts.resistance || 0
    //无视防御
    this.reduceDefend = opts.reduceDefend || 0
    //无视防御百分比
    this.reduceDefendPer = opts.reduceDefendPer || 0
    //吸血百分比
    this.vampirePer = opts.vampirePer || 0
    //每回合回血
    this.recoverHp = opts.recoverHp || 0
    //伤害减免
    this.reduceDamagePer = opts.reduceDamagePer || 0
    //物理伤害减免
    this.reducePhysicalDamagePer = opts.reduceDamagePer || 0
    //技能伤害减免
    this.reduceSkillDamagePer = opts.reduceDamagePer || 0
}

//角色属性
function FightAttri(opts) {
    //血量
    this.hp = opts.hp || 0
    //最大血量
    this.maxHp = opts.maxHp || 0
    //攻击力
    this.attack = opts.attack || 0
    //防御
    this.defend = opts.defend || 0
    //暴击伤害
    this.critHurt = opts.critHurt || 0
    //暴击
    this.crit = opts.crit || 0
    //韧性
    this.toughness = opts.toughness || 0
    //命中
    this.hit = opts.hit || 0
    //闪避
    this.avoid = opts.avoid || 0
    //控制
    this.control = opts.control || 0
    //抗性
    this.resistance = opts.resistance || 0
    //治疗
    this.cure = opts.cure || 0
    //无视防御
    this.reduceDefend = opts.reduceDefend || 0
    //无视防御百分比
    this.reduceDefendPer = opts.reduceDefendPer || 0
    //吸血百分比
    this.vampirePer = opts.vampirePer || 0
    //每回合回血
    this.recoverHp = opts.recoverHp || 0
    //伤害减免
    this.reduceDamagePer = opts.reduceDamagePer || 0
    //物理伤害减免
    this.reducePhysicalDamagePer = opts.reduceDamagePer || 0
    //技能伤害减免
    this.reduceSkillDamagePer = opts.reduceDamagePer || 0
    //战斗状态
    this.battleStatus = opts.battleStatus || 0
    //状态
    this.status = opts.status || 0 //1:无法回血
    //技能序号
    this.skillPos = opts.skillPos || 0
    //战斗位置
    this.pos = opts.pos || 0
    //技能
    this.skills = opts.skills || 0
    //角色名子
    this.name = opts.name || ''
    //类型 1:人物2:宠物：3：怪物
    this.type = opts.type || 3
    //暴击率
    this.critRate = opts.critRate || 0
    //韧性率
    this.toughnessRate = opts.toughnessRate || 0
    //命中率
    this.hitRate = opts.hitRate || 0
    //闪避率
    this.avoidRate = opts.avoidRate || 0
}

//计算概率
function calcRate(x) {
    var array1 = [
        [0, 1, 0],
        [20, 2, 20],
        [60, 3, 40],
        [120, 4, 60],
        [200, 5, 80],
        [300, 6, 100],
        [420, 7, 120],
        [560, 8, 140],
        [720, 9, 160],
        [900, 10, 180],
        [1100, 11, 200],
        [1320, 12, 220],
        [1560, 13, 240]
    ]
    var array2 = [20, 60, 120, 200, 300, 420, 560, 720, 900, 1100, 1320, 1560]
    var index = underscore.sortedIndex(array2, x)
    rate = Math.floor((x - array1[index][0]) / array1[index][1]) + array1[index][2]
    return rate
}

//计算基础属性
function calcBasicAttri(player) {
    //血量
    player.fightAttri.maxHp += player.basicAttri.maxHp
    //攻击力
    player.fightAttri.attack += player.basicAttri.attack
    //防御
    player.fightAttri.defend += player.basicAttri.defend
    //暴击伤害
    player.fightAttri.critHurt += player.basicAttri.critHurt
    //暴击
    player.fightAttri.crit += player.basicAttri.crit
    //韧性
    player.fightAttri.toughness += player.basicAttri.toughness
    //命中
    player.fightAttri.hit += player.basicAttri.hit
    //闪避
    player.fightAttri.avoid += player.basicAttri.avoid
    //控制
    player.fightAttri.control += player.basicAttri.control
    //抗性
    player.fightAttri.resistance += player.basicAttri.resistance
}

//计算属性概率
function calcAllRate(player) {
    player.fightAttri.critRate = calcRate(player.fightAttri.crit)
    player.fightAttri.toughnessRate = calcRate(player.fightAttri.toughness)
    player.fightAttri.hitRate = calcRate(player.fightAttri.hit)
    player.fightAttri.avoidRate = calcRate(player.fightAttri.avoid)
}

//计算人物属性
monster.prototype.calcAttri = function() {
    this.fightAttri = new FightAttri({})
    //人物基础属性
    calcBasicAttri(this)
    //技能
    //装备
    //属性概率
    calcAllRate(this)
    this.fightAttri.hp = this.fightAttri.maxHp
    //技能
    this.fightAttri.skills = this.skills
    this.fightAttri.name = this.name
    this.fightAttri.type = this.type
}

module.exports = monster